﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE.Tool.UserControls;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_ExchangeRateAdd : DevExpress.XtraEditors.XtraForm
	{
		private ucExchangeRate parent;
		public frm_ExchangeRateAdd(ucExchangeRate fExchangeRate)
		{
			InitializeComponent();
			parent = fExchangeRate;
            this.CancelButton = this.btCancel;
            this.ActiveControl = dateExchangeDate;
			
		}

		private void btAdd_Click(object sender, EventArgs e)
		{
			ExchangeRateHelper helper = new ExchangeRateHelper(new ExchangeRates());
			if (GetData(helper.ValueObject))
			{
				Utilities.Timer timer = new Utilities.Timer();

				if (helper.Insert() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.AddSuccess);

				    cmbCurrency.SelectedItem = cmbCurrency.Items[0];
				    txtExchangerRate.Text = "";
				    dateExchangeDate.Value = DateTime.Now;
                    dateExchangeDate.Focus();
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.AddFailed);
				}
				parent._action = 0;
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		#region Private methods

		private bool GetData(ExchangeRates o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(cmbCurrency.SelectedValue))
			{
				MessageBox.Show(ToolConstants.Message.ExchangeRate.CurrentcyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtExchangerRate.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.ExchangeRate.ExchangeRateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(dateExchangeDate.Value.ToShortDateString()))
			{
				MessageBox.Show(ToolConstants.Message.ExchangeRate.ExchangeDateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			#endregion

			o.CurrencyID = int.Parse(cmbCurrency.SelectedValue.ToString());
			o.ExchangeRate = decimal.Parse(txtExchangerRate.Text);
			o.ExchangeDate = dateExchangeDate.Value;

			return true;
		}

		private void BindDataCurrency()
		{

		}

		
		#endregion
	}
}